package com.xxl.job.admin.controller;

import com.xxl.job.admin.controller.annotation.PermissionLimit;
import com.xxl.job.admin.service.LoginService;
import com.xxl.job.admin.service.XxlJobService;
import com.xxl.job.core.biz.model.ReturnT;
import org.noear.solon.annotation.Controller;
import org.noear.solon.annotation.Inject;
import org.noear.solon.annotation.Mapping;
import org.noear.solon.annotation.Post;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.ModelAndView;

import java.util.Date;
import java.util.Map;

/**
 * index controller
 * @author xuxueli 2015-12-19 16:13:16
 */
@Controller
public class IndexController {

	@Inject
	private XxlJobService xxlJobService;
	@Inject
	private LoginService loginService;


	@Mapping("/")
	public ModelAndView index(ModelAndView model) {
		model.view("index.ftl");
		Map<String, Object> dashboardMap = xxlJobService.dashboardInfo();
		model.putAll(dashboardMap);
		return model;
	}

    @Post
    @Mapping("/chartInfo")
	public ReturnT<Map<String, Object>> chartInfo(Date startDate, Date endDate) {
        ReturnT<Map<String, Object>> chartInfo = xxlJobService.chartInfo(startDate, endDate);
        return chartInfo;
    }
	
	@Mapping("/toLogin")
	@PermissionLimit(limit=false)
	public ModelAndView toLogin(Context context, ModelAndView modelAndView) {
		if (loginService.ifLogin(context) != null) {
			context.forward("/");
			return null;
		}else {
			modelAndView.view("login.ftl");
			modelAndView.put("context", Context.current());
			return modelAndView;
		}
	}

	@Post
	@Mapping(value="login")
	@PermissionLimit(limit=false)
	public ReturnT<String> loginDo(Context context, String userName, String password, String ifRemember){
		boolean ifRem = (ifRemember!=null && ifRemember.trim().length()>0 && "on".equals(ifRemember))?true:false;
		return loginService.login(context, userName, password, ifRem);
	}

	@Post
	@Mapping(value="logout")
	@PermissionLimit(limit=false)
	public ReturnT<String> logout(Context context){
		return loginService.logout(context);
	}
	
	@Mapping("/help")
	public String help() {

		/*if (!PermissionInterceptor.ifLogin(request)) {
			return "redirect:/toLogin";
		}*/

		return "help";
	}
}
